package com.jiuyun.dao.impl;

import com.jiuyun.dao.StudentDao;
import com.jiuyun.entity.Student;
import com.jiuyun.util.Alipool;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    QueryRunner runner=new QueryRunner();
    @Override
    public List<Student> getUsers() throws SQLException {
        Connection conn= Alipool.getConn();
        List list=new ArrayList();
        String sql="SELECT * FROM student";
        PreparedStatement pst=conn.prepareStatement(sql);
        ResultSet rs=pst.executeQuery();
        while (rs.next()){
            int id=rs.getInt(1);
            int cid=rs.getInt(2);
            String stname=rs.getString(3);
            String stpass=rs.getString(4);
            Student student=new Student(id,cid,stname,stpass);
            list.add(student);
        }
        return list;
    }

    @Override
    public int add(Student s) throws SQLException {
        Connection conn=Alipool.getConn();
        String sql="insert into Student(classid,stname,stpass) values(?,?,?)";
        int n=runner.update(conn,sql,s.getClassid(),s.getStname(),s.getStpass());
        conn.close();
        return n;
    }
}
